import axios from "axios"
import Vue from "vue"
import router from "../router"
import qs from "querystring"

import { Toast } from 'vant';
//环境配置
if (process.env.NODE_ENV == "development") {
    // 开发环境
    Vue.prototype.$pre = "http://localhost:3000"
} else {
    // 生产环境
    Vue.prototype.$pre = ""
}

axios.interceptors.request.use((req) => {
    console.log("=======请求拦截===============");

    //如果请求的地址不是登录也不是注册，那么就携带token
    if (req.url != "/api/login" && req.url != "/api/register") {
        req.headers.authorization = JSON.parse(localStorage.getItem("user")).token
    }
    return req
});
//响应拦截
axios.interceptors.response.use((res) => {
    //1.打印数据
    console.log("======响应拦截============");
    console.log("本次请求地址是：" + res.config.url);
    console.log(res);

    // 2.统一处理失败
    if (res.data.code !== 200) {
        Toast(res.data.msg);

    }

    // 3.掉线处理
    if (res.data.msg == "登录已过期或访问权限受限") {
        //跳转到登录页
        router.push("/login")
    }


    return res;
})

//登录
export let reqLogin = (user) => {
    return axios({
        url: "/api/login",
        method: "post",
        data: user,
    })
}


//注册
export let reqRegsiter = (user) => {
    return axios({
        url: "/api/register",
        method: "post",
        data: user
    })

}

// 首页轮播图
export let reqSide = () => {
    return axios({
        url: "/api/getbanner"
    })
}

//首页限时秒杀

export let reqSeck = () => {
    return axios({
        url: "/api/getseckill"
    })
}

//首页商品

export let reqGood = () => {
    return axios({
        url: "/api/gethortgoods"
    })
}

//搜索页面

export let reqSearch = (user) => {
    return axios({
        url: "/api/search",
        params: user
    })
}

//一级分类

export let reqCates = (user) => {
    return axios({
        url: "/api/getcates"
    })
}

//一级分类

export let reqTolist = (user) => {
    return axios({
        url: "/api/getgoodlist",
        params: user
    })
}

//获去单个信息

export let reqsinfo = (user) => {
    return axios({
        url: "/api/getgoodsinfo",
        params: user
    })
}

//添加到购物车

export let reqcart = (user) => {
    return axios({
        url: "/api/cartadd",
        method: "POST",
        data: user
    })
}

//购物车获取

export let reqCartlist = (user) => {
    return axios({
        url: "/api/cartlist",
        params: user
    })
}


// 购物车删除
export const reqShopDel = (data) => {
    return axios({
        url: "/api/cartdelete",
        method: "post",
        data: qs.stringify(data)
    })
}



// 购物车修改
export const reqShopUpdate = (data) => {
    return axios({
        url: "/api/cartedit",
        method: "post",
        data: qs.stringify(data),

    })
}

//获取地址列表
export const reqSitelist = (params) => {
    return axios({
        url: "/api/addresslist",
        params
    })
}

//添加地址

export const reqAddSite = (data) => {
    return axios({
        url: "/api/addressadd",
        method: "post",
        data
    })
}

//删除地址

export const reqDelSite = (data) => {
    return axios({
        url: "/api/addressremove",
        method: "post",
        data
    })
}


//编辑地址

export const reqRessSite = (data) => {
    return axios({
        url: "/api/addressedit",
        method: "post",
        data
    })
}

//提交订单


export const reqSubmit = (data) => {
    return axios({
        url: "/api/orderadd",
        method: "post",
        data: qs.stringify(data)
    })
}